﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
using Newtonsoft.Json;

namespace Sales
{
    public partial class frmGathering2 : Form
    {
        public static string config_dir = Application.StartupPath + "/config";
        public static string config_file = config_dir + "/config.xml";
        public SysConfig config;
        public Common2 common = new Common2();
        private static frmGathering2 gathering2 = null;
        private bool hit = false;
        public frmGathering2()
        {
            this.IsEnter = false;
            InitializeComponent();
            config = Common2.GetConfig( );//获取相关配置属性 
            IsChoice = config.IsChoice;
            MiddleModule.eventSend += new Send(MiddleModule_eventSend);
        }
        #region 页面属性
        /// <summary>
        /// 应收
        /// </summary>
        public string Ys
        {
            get;
            set;
        }
        /// <summary>
        /// 现金
        /// </summary>
        public string Xj
        {
            get;
            set;
        }
        /// <summary>
        /// 找零
        /// </summary>
        public string Zl
        {
            get;
            set;
        }
        public int IsChoice
        {
            get;
            set;
        }


        /// <summary>
        /// 欠款
        /// </summary>
        public string Qk
        {
            get;
            set;
        }

        public SaleStatus saleEntity
        {
            get; set;
        }

        /// <summary>
        /// 是否敲回车
        /// </summary>
        public bool IsEnter { get; set; }
        #endregion
        private void gbtncancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public static frmGathering2 CreateInstance()
        {
            if (null == gathering2)
            {
                gathering2 = new frmGathering2();
            }
            return gathering2;
        }


        void MiddleModule_eventSend(object sender, object receiver, object msg)
        {
            frmMain frmMain = sender as frmMain;
            if (null != frmMain && gathering2.ToString() == receiver.ToString())
            {
                SaleStatus model = JsonConvert.DeserializeObject<SaleStatus>(msg.ToString());
                if (model != null)
                {
                    saleEntity = model;
                    decimal _ys = model.YS;

                    //是否四舍五入
                    if (this.IsChoice > 0)
                        _ys = Math.Round(_ys, 0, MidpointRounding.AwayFromZero);

                    this.ctxtYs.Text = _ys.ToString("0.00");
                    Ys = _ys.ToString("0.00");


                    this.ctxtQk.Text = ""; //(-decimal.Parse(strYs)).ToString("0.00");
                    this.txtXj.Text = "";

                    this.ctxtWx.Text = "";
                    this.ctxtZfb.Text = "";
                    this.ctxtYhk.Text = "";
                    this.ctxtDjq.Text = "";



                    this.ctxtZl.Text = "0.00";
                }


            }
        }


        private void txtXj_TextChanged(object sender, EventArgs e)
        {
            Calc();
        }

        private void txtXj_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }

        #region 热键设置



        private void KeyDownEnter()
        {

            decimal dys = 0;//应收金额
            decimal dss = 0;
            if (Ys != "")
                dys = decimal.Parse(Ys);
            Sale model = Calc();

            if (model.Ss != "")
                dss = decimal.Parse(model.Ss);
            decimal dzk = 0;

            string zk = this.ctxtQk.Text;
            if (zk != "")
                dzk = decimal.Parse(zk);

            model.DZk = dzk;
            if (saleEntity != null && saleEntity.TH)
            {
                if (dss == dys)
                {
                    string jsonGroup = JsonConvert.SerializeObject(model);

                    frmMain main = new frmMain();
                    MiddleModule.SendMessage(this, main, jsonGroup);
                    this.Close();
                }
                else
                {
                    this.IsEnter = true;
                    MessageBox.Show("输入金额必须等于应收金额！");
                }
            }
            else
            {
                if (dys > 0 && dss > 0 && dss >= (dys - dzk))
                {
                    string jsonGroup = JsonConvert.SerializeObject(model);

                    frmMain main = new frmMain();
                    MiddleModule.SendMessage(this, main, jsonGroup);
                    this.Close();
                }
                else
                {
                    this.IsEnter = true;
                    MessageBox.Show("输入金额必须等于应收金额！");
                }
            }





        }

        private Sale Calc()
        {
            string xj = this.txtXj.Text.Trim();//现金
            string wx = this.ctxtWx.Text.Trim();//微信
            string zfb = this.ctxtZfb.Text.Trim();//支付宝
            string yhk = this.ctxtYhk.Text.Trim();//银行卡
            string djq = this.ctxtDjq.Text.Trim();//代金券


            string zk = this.ctxtQk.Text.Trim();//折扣


            decimal dys = 0;


            decimal dxj = 0;//现金
            decimal dwx = 0;//微信
            decimal dzfb = 0;//支付宝
            decimal dyhk = 0;//银行卡
            decimal ddjq = 0;//代金券

            decimal dzk = 0;
            if (Ys != "" && Ys != "-")
                dys = decimal.Parse(Ys);
            //普通消费
            if (xj != "" && xj != "-")
                dxj = decimal.Parse(xj);
            //微信消费
            if (wx != "" && wx != "-")
                dwx = decimal.Parse(wx);
            //支付宝消费
            if (zfb != "" && zfb != "-")
                dzfb = decimal.Parse(zfb);
            //银行卡消费
            if (yhk != "" && yhk != "-")
                dyhk = decimal.Parse(yhk);
            //代金券消费
            if (djq != "" && djq != "-")
                ddjq = decimal.Parse(djq);

            if (zk != "" && zk != "-")
                dzk = decimal.Parse(zk);



            decimal all = dxj + dwx + dzfb + dyhk + ddjq;//总计现金

            decimal all_1 = dxj + dwx + dzfb + dyhk;//总计现金

            decimal zl = all - (dys - dzk);
            if (zl > 0)
                this.ctxtZl.Text = zl.ToString();
            else
                this.ctxtZl.Text = "0";


            Sale model = new Sale();
            model.Xj = xj;
            model.Wx = wx;
            model.Yhk = yhk;
            model.Ys = Ys;
            model.Zfb = zfb;
            model.Zk = zk;
            model.Zl = ctxtZl.Text;

            model.Ss = all.ToString();
            model.SsJe = all_1.ToString();
            return model;
        }


        /// 重载FromA中的WndProc函数
        /// 监视Windows消息
        /// 重载WndProc方法，用于实现热键响应
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //按快捷键
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 105:
                            KeyDownEnter();
                            break;
                        case 201:
                            //设置光标位置
                            this.txtXj.Focus();
                            break;
                        case 202:
                            //设置光标位置
                            this.ctxtWx.Focus();
                            break;
                        case 203:
                            //设置光标位置
                            this.ctxtZfb.Focus();
                            break;
                        case 204:
                            //设置光标位置
                            this.ctxtYhk.Focus();
                            break;
                        case 205:
                            //设置光标位置
                            this.ctxtDjq.Focus();
                            break;
                        case 206:
                            //设置光标位置
                            this.ctxtQk.Focus();
                            break;
                    }
                    break;
            }

            base.WndProc(ref m);
        }
        #endregion

        private void frmGathering2_Activated(object sender, EventArgs e)
        {
            HotKey.RegisterHotKey(Handle, 105, HotKey.KeyModifiers.None, Keys.Enter);
            HotKey.RegisterHotKey(Handle, 201, HotKey.KeyModifiers.Alt, Keys.D1);//光标进入 付现文本框
            HotKey.RegisterHotKey(Handle, 202, HotKey.KeyModifiers.Alt, Keys.D2);//光标进入 微信文本框
            HotKey.RegisterHotKey(Handle, 203, HotKey.KeyModifiers.Alt, Keys.D3);//光标进入 支付宝文本框
            HotKey.RegisterHotKey(Handle, 204, HotKey.KeyModifiers.Alt, Keys.D4);//光标进入 银行卡文本框
            HotKey.RegisterHotKey(Handle, 205, HotKey.KeyModifiers.Alt, Keys.D5);//光标进入 代金券文本框
            HotKey.RegisterHotKey(Handle, 206, HotKey.KeyModifiers.Alt, Keys.Z);//光标进入                                                                      //设置光标位置
            this.txtXj.Focus();
        }

        private void frmGathering2_Leave(object sender, EventArgs e)
        {
            //注销Id号为105的热键设定
            HotKey.UnregisterHotKey(Handle, 105);
            HotKey.UnregisterHotKey(Handle, 201);
            HotKey.UnregisterHotKey(Handle, 202);
            HotKey.UnregisterHotKey(Handle, 203);
            HotKey.UnregisterHotKey(Handle, 204);
            HotKey.UnregisterHotKey(Handle, 205);
            HotKey.UnregisterHotKey(Handle, 206);
        }

        private void ctxtQk_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }

        private void cTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }

        private void ctxtWx_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }

        private void ctxtZfb_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }

        private void ctxtYhk_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }

        private void ctxtDjq_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }

        private void gbtnSK_Click(object sender, EventArgs e)
        {
            KeyDownEnter();
        }

        private void ctxtWx_TextChanged(object sender, EventArgs e)
        {
            Calc();
        }

        private void ctxtZfb_TextChanged(object sender, EventArgs e)
        {
            Calc();
        }

        private void ctxtYhk_TextChanged(object sender, EventArgs e)
        {
            Calc();
        }

        private void ctxtDjq_TextChanged(object sender, EventArgs e)
        {
            Calc();
        }

        private void ctxtQk_TextChanged(object sender, EventArgs e)
        {
            Calc();
        }
    }
}
